package com.hikvision.open.app.ui.preview

import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.hikvision.open.app.utils.BarUtils
import com.hikvision.open.app.utils.ScreenUtils
import com.hikvision.open.app.utils.inflate
import com.hikvision.open.app.widget.window.WindowGroup
import hik.sdk.demo.R
import hik.sdk.demo.databinding.ActivityPreviewBinding

/**
 * <p> 预览界面 </p>
 * @author 段自航 2021/7/14 10:20
 * @version V1.0
 */
class PreviewActivity : AppCompatActivity() {

    private val viewBinding: ActivityPreviewBinding by inflate()

    private val groupHelper by lazy {
        viewBinding.windowGroup.windowGroupAdapter
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(viewBinding.root)
        initTitle()
        initWindowGroup()
    }

    private fun initTitle() {
        viewBinding.backImage.setOnClickListener {
            finish()
        }
        viewBinding.modeOneButton.setOnClickListener {
            groupHelper.windowMode = WindowGroup.WINDOW_MODE_ONE
        }
        viewBinding.modeFourButton.setOnClickListener {
            groupHelper.windowMode = WindowGroup.WINDOW_MODE_FOUR
        }
    }


    private fun initWindowGroup() {
        //默认选中第一个窗口
        viewBinding.windowGroup.selectedCurrFirstWindow()
        groupHelper.windowItemStructAllList.forEach {
            //设置点击事件
            it.setOnSingleClickListener { lastWindowSerial, currentWindowSerial ->
                //上一次选中的窗口序列号和当前选中窗口序列号不一致
                if (lastWindowSerial != currentWindowSerial) {
                    return@setOnSingleClickListener
                }
                //判断是否展示
                if (viewBinding.autoHideView.isVisible) {
                    viewBinding.autoHideView.hide()
                } else {
                    viewBinding.autoHideView.show()
                }
            }
        }
        viewBinding.previewControlView.setWindowGroupHelper(groupHelper)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        layoutViews()
    }

    private fun layoutViews() {
        if (ScreenUtils.isPortrait()) {
            viewBinding.titleGroup.isVisible = true
            viewBinding.windowGroup.setBackgroundResource(R.color.portrait_windowgroup_bg)
            BarUtils.setStatusBarVisibility(this, true)
            viewBinding.previewControlView.setPrivateFatio(1.0f)
        } else {
            viewBinding.titleGroup.isVisible = false
            viewBinding.windowGroup.setBackgroundResource(R.color.land_windowgroup_bg)
            BarUtils.setStatusBarVisibility(this, false)
            viewBinding.previewControlView.setPrivateFatio(0.2f)
        }
    }

    override fun onRestart() {
        super.onRestart()
        //界面重新展示时，通知窗口可见了
        groupHelper.setPageChangeEvent(groupHelper.currentPage, groupHelper.windowMode)
    }


    override fun onStop() {
        super.onStop()
        //界面到后台时，通知窗口不可见了
        groupHelper.windowItemStructAllList.forEach {
            if (it.userVisibleHint) {
                it.userVisibleHint = false
            }
        }
    }
}